#include "stdafx.h"
#include "Bullet.h"

#include "AfterBurnerClone.h"
#include "Particles/ParticleSystem.h"

namespace MyGame {

Bullet::Bullet(Game* game) : GameComponent(game) {
    this->direction = D3DXVECTOR3(0, 0, 1);
    this->prevPosition = D3DXVECTOR3(0, 0, 0);
    this->speed = 70;
    this->timer = 1.4f;

    ParticleSystem* particleSystem = ((AfterBurnerClone*)this->GetGame())->GetParticleSystem();
    this->particle = new Particle();
    this->particle->BeginScale = D3DXVECTOR3(0.1f, 3.0f, 1.0f);
    this->particle->Effect = particleSystem->GetBulletParticle();
    this->particle->EndScale = D3DXVECTOR3(0.1f, 3.0f, 1.0f);
    particleSystem->Add(this->particle);
}

Bullet::~Bullet(void) {
    ((AfterBurnerClone*)this->GetGame())->GetParticleSystem()->Remove(this->particle);
    d3d::Delete(this->particle);
}

D3DXVECTOR3 Bullet::GetDirection() const {
    return this->direction;
}

void Bullet::SetDirection(D3DXVECTOR3 val) {
    D3DXVec3Normalize(&this->direction, &val);
}

D3DXVECTOR3 Bullet::GetPosition() const {
    return this->position;
}

void Bullet::SetPosition(D3DXVECTOR3 val) {
    this->position = val;
}

D3DXVECTOR3 Bullet::GetPrevPosition() const {
    return this->prevPosition;
}

bool Bullet::IsAlive() const {
    return this->timer > 0;
}

void Bullet::Update(FLOAT timeDelta) {
    __super::Update(timeDelta);

    this->timer -= timeDelta;
    this->prevPosition = this->GetPosition();
    D3DXVECTOR3 newPosition = this->prevPosition + direction * this->speed * timeDelta;
    this->SetPosition(newPosition);
    this->particle->Position = newPosition;
}

}
